[LegacyColorValue = true]; 

{ TSM Swing: Swing trading system
  Copyright 1994-1999, P.J. Kaufman.  All rights reserved. 
  The swing high and low points can be plotted using "TMS Swing"

  Inputs for TSM SWING:
	type 	= 0, normal price
			= 1, 3-month rate
			= 2, bond price at par
     swing 	= price swing in whole % (e.g., 2% = 2.0)  }

   input: type(0), swing(2.5);
   vars:  pcswing(0), last(0), curhigh(0), curlow(0), swhigh(0), swlow(0) , highbar(0),
		lowbar(0), chighbar(0), clowbar(0), lowp(0), highp(0), xclose(0), xhigh(0),
		xlow(0), divide(4), par(800), prevhigh(0), prevlow(0);

   pcswing = swing / 100.;

	if type = 0 then begin
			xhigh = high;
			xlow = low;
			xclose = close;
			end
		else if type = 1 then begin
			xhigh = 100 - low;
			xlow = 100 - high;
			xclose = 100 - close;
			end
		else if type = 2 then begin
			xhigh = par / low;
			xlow = par / high;
			xclose = par / close;
			end;

{ INITIALIZE MOST RECENT HIGH AND LOW }
  if currentbar = 1 then begin
      curhigh = xclose;			{ current high }
      curlow  = xclose;			{ current low }
      end;

{ Buy or sell signals }
	if type = 0 then begin
	   if marketposition <= 0 and xhigh > swhigh then Buy This Bar  on close
		else if marketposition >= 0 and xlow < swlow then Sell Short This Bar  on close;
		end
	else begin
	   if marketposition >= 0 and xhigh > swhigh then Sell Short This Bar  on close
		else if marketposition <= 0 and xlow < swlow then Buy This Bar  on close;
		end;
                
{ SEARCH FOR A NEW HIGH -- favor reversals }
   if last<>1 then begin
   { REVERSE FROM HIGH IF MINIMUM % SWING }
      if xlow < curhigh - curhigh*pcswing then begin
         last = 1;			{ last high fixed }
         swhigh   = curhigh;		{ new verified high }
         highbar   = chighbar;
         curlow = xlow;			{ initialize new lows }
         lowp = xlow;			{ swing low for plot }
         clowbar = currentbar;
         end
      else begin
      if xhigh > curhigh then begin
         curhigh = xhigh;          { new current high }
         chighbar = currentbar;
         end;
      end;
   end;

{ SEARCH FOR A NEW LOW - favor reversal }
   if last <> -1 then begin
{ REVERSAL FROM LOW IF MINIMUM % SWING }
   if xhigh > curlow + curlow*pcswing then begin
      last = -1;
      swlow   = curlow;
      lowbar   = clowbar;
      curhigh = xhigh;          { initialize current high }
      highp = xhigh;               { swing high for plot }
      chighbar = currentbar;
      end
   else begin
      if xlow < curlow then begin
         curlow = xlow;
         clowbar = currentbar;
         end;
      end;
   end;